package com.maple.common.widget

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.maple.common.R
import com.maple.common.base.BaseQuickBindAdapter
import com.maple.common.databinding.WidgetBottomSheetDialogBinding
import com.maple.common.ext.click
import com.maple.common.ext.divider
import com.maple.common.ext.gone
import com.maple.common.ext.invisible
import com.maple.common.ext.screenWidth
import com.maple.common.ext.visible
import com.maple.common.ext.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout


/**
 * Tip:
 * Create by SeVen on 2024/4/15 21:42
 */
class BottomSheetWidget<T : Any, VH : RecyclerView.ViewHolder>(
    context: Context
) :
    BottomSheetDialog(context, R.style.BottomSheetDialog) {

    private var onLoadMoreListener: ((refreshLayout: RefreshLayout) -> Unit)? = null

    private var onLeftClickListener: (() -> Unit)? = null

    private var onRightClickListener: ((adapter: BaseQuickBindAdapter<T, VH>?) -> Unit)? = null

    private var bottomSheetAdapter: BaseQuickBindAdapter<T, VH>? = null

    private val binding = WidgetBottomSheetDialogBinding.bind(
        LayoutInflater.from(context).inflate(R.layout.widget_bottom_sheet_dialog, null)
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        val layoutParams = binding.llRoot.layoutParams
        layoutParams.width = screenWidth
        layoutParams.height = dpToPx(context, 336)
        binding.llRoot.layoutParams = layoutParams
        this.setCancelable(false)

        initView()
        initData()
        initListener()
    }

    private fun initView() {
        binding.refreshRecycle.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            divider(context.getColor(R.color.common_underline_color))
            setHasFixedSize(true)
//            isNestedScrollingEnabled = false
        }
    }

    private fun initData() {

    }

    private fun initListener() {
        binding.btnLeft.click {
            onLeftClickListener?.invoke() ?: kotlin.run {
                dismiss()
            }
        }

        binding.btnRight.click {
            onRightClickListener?.invoke(bottomSheetAdapter)
        }
    }

    fun setTitle(title: String) {
        binding.tvTitle.text = title
    }

    fun setRightVisible(visible: Boolean) {
        if (visible) {
            binding.btnRight.visible()
        } else {
            binding.btnRight.invisible()
        }
    }

    fun setLeft(left: String) {
        binding.btnLeft.text = left
    }

    fun setRight(right: String) {
        binding.btnLeft.text = right
    }

    fun setLeftVisible(visible: Boolean) {
        if (visible) {
            binding.btnLeft.visible()
        } else {
            binding.btnLeft.invisible()
        }
    }


    fun setAdapter(bottomSheetAdapter: BaseQuickBindAdapter<T, VH>?) {
        this.bottomSheetAdapter = bottomSheetAdapter
        binding.refreshRecycle.adapter = this.bottomSheetAdapter
    }

    fun setEmptyData(isEmpty: Boolean) {
        if (isEmpty) {
            binding.refreshRecycle.gone()
            binding.emptyDataView.visible()
        } else {
            binding.refreshRecycle.visible()
            binding.emptyDataView.gone()
        }
    }

    fun setData(list: MutableList<T>) {
        bottomSheetAdapter?.addAll(list)
    }

    fun setNewData(list: MutableList<T>) {
        bottomSheetAdapter?.submitList(list)
    }

    fun setOnLoadMoreListener(onLoadMoreListener: ((refreshLayout: RefreshLayout) -> Unit)? = null) {
        this.onLoadMoreListener = onLoadMoreListener
    }


    fun setLeftClickListener(onLeftClickListener: (() -> Unit)? = null) {
        this.onLeftClickListener = onLeftClickListener
    }

    fun setRightClickListener(onRightClickListener: ((adapter: BaseQuickBindAdapter<T, VH>?) -> Unit)? = null) {
        this.onRightClickListener = onRightClickListener
    }
}